﻿myApp.controller("propertyController", function ($scope, $http, $modal, modalService, $routeParams, $location, $filter,$compile, NgMap) {
    
    $scope.share = {};
    NgMap.getMap().then(function (map) {
        $scope.map = map;
    });

   
    $scope.showProperty = function (evt, data) {
        $scope.store = data;
        console.log(data);
        if ($routeParams.listType.substring(0, 1) == 'S') {           
            $scope.store.displayPrice =  "Sale Price : RM" + data.SalePrice.toLocaleString();
        } else {
            $scope.store.displayPrice =  "Rental Price : RM" + data.RentPrice.toLocaleString();
        }
        $scope.map.showInfoWindow('foo', this);
    };

    $scope.showInfoWindow = function (evt, storeId) {
        //$scope.store = $scope.stores[storeId];
        console.log("google showInfoWindow");
        console.log(this);
        console.log(storeId);
        console.log(evt);
        $scope.map.showInfoWindow('foo', this);
    };
    $scope.mouseover = function () {
        console.log('mouseover');
    };
    function pageInit() {
        

        if ($routeParams.id) {
            // get from session storage
            // if there is data in session
            var data = sessionStorage.getItem($routeParams.id);
            

            // if no data in session, get from the server
            jsonObj = JSON.parse(data);
            $scope.property = jsonObj;
           // console.log($scope.property);
            var url = 'api/Property/GetPropertyDetailByListType/' + $routeParams.id + '/' + $routeParams.listType;
            $http.get(url).success(function (data) {
                $scope.property = data.propertyDetail;

                console.log("this is all the data ", $scope.property);
             
                $scope.property.listType = $routeParams.listType;

                var transactStr;
                if ($routeParams.listType.substring(0, 1) == 'S') {
                    $scope.property.transact = 'S';                    
                    $scope.nearByList = data.nearByListSale;
                    var toBeFilter = $filter('filter')($scope.nearByList, { propertyid: $routeParams.id });
                    var index = $scope.nearByList.indexOf(toBeFilter[0]);
                    $scope.nearByList.splice(index, 1);
                    transactStr = " FOR SALE" + "\n" + "Sale Price : RM" + $scope.property.SalePrice.toLocaleString();
                } else {
                    $scope.property.transact = 'R';
                    $scope.nearByList = data.nearByListRent;
                    transactStr = " FOR RENT" + "\n" + "Rental Price : RM" + $scope.property.RentPrice.toLocaleString();
                }
               // $scope.property.ListType = $scope.property.ListType.toString();
                $scope.slides = data.pictureList;
                if (!$scope.slides) {
                    $scope.slides = [];
                    $scope.slides.push({ pictureURL: 'http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png' });
                }
               // console.log($scope.property.StaffPicURL);
               // $scope.slides.push({ pictureURL: $scope.property.StaffPicURL });

                $scope.share.url = 'http://www.penangrealty.my/Property/' + $routeParams.id.toString() + '/' + $routeParams.listType.toString();
                $scope.share.pictureUrl = $scope.slides ? $scope.slides[0].pictureURL : 'http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png';
                $scope.share.title = $scope.property.LongHeading;
                $scope.share.description = "[" + $scope.property.location + '] ' 
                    + transactStr + '\n' //+ window.location.href + '\n'
                    + "Contact: " + $scope.property.mobile + " (" + $scope.property.agentname + ")";

                document.title = "Penang Realty 世界地產 " + $scope.share.title;

                $scope.$emit('newPageLoaded', { 'title': $scope.share.title, 'description': $scope.share.description });

                $scope.loader = { loading: false };
            }).error(function(data){
                console.log(data);
                $scope.loader = { loading: false };
            })
        }
    }

    pageInit();

});